
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * banner图管理
 * @author lxn
*/
class Banner extends Admin_Controller
{
    private static $data = array();
    protected $models=['banner_model'];
    public function __construct()
    {
        parent::__construct();
        
    }
    //项目主页轮播图
    public function index(){
        $this->load->view('admin/banner/list.html');
    }
    
    public function getBanner(){
        $bannerList = $this->banner_model->getList(['typeFlag'=>2]);        
        show200($bannerList);
    }
    
    /**
     * 添加/修改页
     */
    public function edit()
    {
        $bannerId = $this->input->get_post('bannerId');  
        if($bannerId){
            $bannerInfo = $this->banner_model->getWhereRow(['bannerId'=>$bannerId]);
            self::$data['bannerInfo'] = $bannerInfo;
        }        
       
        $this->load->view('admin/banner/add.html', self::$data);
    }    
    
    //保存
    public function save(){
        $data=$this->input->post();
        if(isset($data['file'])){
            unset($data['file']);
        }
        $data['typeFlag']=2;
        if($data['bannerId']){
            $res=$this->banner_model->updateWhere(['bannerId'=>$data['bannerId']],$data);
            
        }else{
            $res=$this->banner_model->insert($data);
        }
        if ($res){
            show200('操作成功');
        }else{
            show400('操作失败');
        }
    }
    
    /**
     * 删除
     */
    public function del()
    {
        $bannerIds=$this->input->post('bannerIds');
        $res=$this->banner_model->delWhere_in('bannerId',$bannerIds);        //以后再加删除oss上的图片方法
        if($res){
            show200('删除成功');
        }else{
            show400('删除失败');
        }
    }
    
    /**
     * 删除图片
     * */
    public function removeImage( $type = 'imagePath' )
    {
        $this->load->library('obs_upload/obs_upload');      //华为云盘删除方法
        
        $bannerId = intval($this->input->post('bannerId'));
        $imagePath=$this->input->post('imagePath');
        $bannerInfo = $this->banner->getBannerRow($bannerId);
        if($bannerId>0){
            if( ! empty($bannerInfo['imagePath']))
            {
                //@unlink(S_ROOT.'../../'.$bannerInfo['imagePath']);
                $this->obs_upload->del($bannerInfo['imagePath']);
            }
             
            $update_array = array('imagePath' => '');            
            $this->banner->setBannerByID($update_array, $bannerId);
        }else{
            //@unlink(S_ROOT.'../../'.$imagePath);
            $this->obs_upload->del($imagePath);
        }
    }
}